#include <aknglobalnote.h>
#include "FunTrace.h"


static void PrintNoteL(TRefByValue<const TDesC> aFormat, ...)
    {
    VA_LIST list;
    VA_START(list, aFormat);
    TBuf<256> buf;
    buf.FormatList(aFormat, list);
    VA_END(list);
	CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
	globalNote->ShowNoteL(EAknGlobalInformationNote, buf); 
    CleanupStack::PopAndDestroy(globalNote);
    }

RChunk sChunk;
TPtr8 sDataChunk(NULL, 0);

_LIT8(kEnterFormat, "%Ld\t\t%sentering %s");
_LIT8(kExitFormat, "%Ld\t%Ld\t%sexiting %s");
char kIndent[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
TInt TFunTrace::sIndent = 0;
static TInt64 sTimeOffset = 0;

TThreadId sThreadId;

TFunTrace::TFunTrace(const char* aFunName) : iFunName(aFunName)
	{
	if(iFileLogger && RThread().Id() != sThreadId) return;
	
	TTime currentTime;
	currentTime.UniversalTime();
	iStartTime = currentTime;
	iStartTime -= TTimeIntervalMicroSeconds(sTimeOffset);
	++sIndent;
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = 0;
	if(iFileLogger)
		{
		iFileLogger->WriteFormat(kEnterFormat, iStartTime.Int64()/1000, kIndent, iFunName);
		//RDebug::Print(_L("sDataChunk: enter"));
		//sDataChunk.Format(_L8("entering %s"), iFunName);
		//sDataChunk.ZeroTerminate();
		//RDebug::Print(_L("sDataChunk: leave"));
		//ptr.Format(kEnterFormat, iStartTime.Int64()/1000, kIndent, iFunName);
		//ptr.ZeroTerminate();
		}
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = '\t';
	TTime endTime;
	endTime.UniversalTime();
	sTimeOffset += endTime.MicroSecondsFrom(currentTime).Int64();
	}
TFunTrace::~TFunTrace()
	{
	if(iFileLogger && RThread().Id() != sThreadId) return;
			
	TTime currentTime;
	currentTime.UniversalTime();
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = 0;
	if(iFileLogger)
		{
		iFileLogger->WriteFormat(kExitFormat, (currentTime - TTimeIntervalMicroSeconds(sTimeOffset)).Int64()/1000, ((currentTime - TTimeIntervalMicroSeconds(sTimeOffset)).MicroSecondsFrom(iStartTime).Int64())/1000, kIndent, iFunName);
		//sDataChunk.Format(_L8("leaving %s"), iFunName);
		//sDataChunk.ZeroTerminate();
		//ptr.Format(kEnterFormat, iStartTime.Int64()/1000, kIndent, iFunName);
		//ptr.ZeroTerminate();
		}
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = '\t';
	--sIndent;
	TTime endTime;
	endTime.UniversalTime();
	sTimeOffset += endTime.MicroSecondsFrom(currentTime).Int64();
	}
	
RFileLogger* TFunTrace::iFileLogger;

void TFunTrace::setFileLogger(RFileLogger* aFileLogger)
	{
	iFileLogger = aFileLogger;
	
	sThreadId = RThread().Id();

	/*
	TInt ret = sChunk.CreateGlobal(_L("SingaporeBus"), 10000, 10000);
	if(ret == KErrAlreadyExists)
		ret = sChunk.OpenGlobal(_L("SingaporeBus"), EFalse);
	sDataChunk.Set(sChunk.Base(), 0, 10000);*/
	
	}

/*
#include "FunTrace.h"
#include <coemain.h>


_LIT8(kEnterFormat, "%Ld\t\t%sentering %s");
_LIT8(kExitFormat, "%Ld\t%Ld\t%sexiting %s");
char kIndent[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
TInt TFunTrace::sIndent = 0;
static TInt64 sTimeOffset = 0;
RFile TFunTrace::iFileLog;
TFunTrace::TFunTrace(const char* aFunName) : iFunName(aFunName)
	{
	TTime currentTime;
	currentTime.UniversalTime();
	iStartTime = currentTime;
	iStartTime -= TTimeIntervalMicroSeconds(sTimeOffset);
	++sIndent;
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = 0;
	if(iFileLogger)
		{
		iFileLogger->WriteFormat(kEnterFormat, iStartTime.Int64()/1000, kIndent, iFunName);
		TFunTrace::iFileLog.Write(_L8("Enter "));
		TInt strlen = 0;
		for( ; iFunName[strlen] ; ++strlen);
		TFunTrace::iFileLog.Write( TPtrC8((const TUint8*)iFunName, strlen) );
		TFunTrace::iFileLog.Write(_L8("\n"));
		}
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = '\t';
	TTime endTime;
	endTime.UniversalTime();
	sTimeOffset += endTime.MicroSecondsFrom(currentTime).Int64();
	}
TFunTrace::~TFunTrace()
	{
	TTime currentTime;
	currentTime.UniversalTime();
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = 0;
	if(iFileLogger)
		{
		iFileLogger->WriteFormat(kExitFormat, (currentTime - TTimeIntervalMicroSeconds(sTimeOffset)).Int64()/1000, ((currentTime - TTimeIntervalMicroSeconds(sTimeOffset)).MicroSecondsFrom(iStartTime).Int64())/1000, kIndent, iFunName);
		TFunTrace::iFileLog.Write(_L8("Leave "));
		TInt strlen = 0;
		for( ; iFunName[strlen] ; ++strlen);
		TFunTrace::iFileLog.Write( TPtrC8((const TUint8*)iFunName, strlen) );
		TFunTrace::iFileLog.Write(_L8("\n"));
		}
	if(sIndent < sizeof(kIndent)) kIndent[ sIndent ] = '\t';
	--sIndent;
	TTime endTime;
	endTime.UniversalTime();
	sTimeOffset += endTime.MicroSecondsFrom(currentTime).Int64();
	}
	
RFileLogger* TFunTrace::iFileLogger;
void TFunTrace::setFileLogger(RFileLogger* aFileLogger)
	{
	iFileLogger = aFileLogger;
	iFileLog.Replace(CCoeEnv::Static()->FsSession(), _L("C:\\Data\\logs.txt"), EFileWrite);
	}
 */
